package cn.richx.pushmessage.domain;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 区域
 * @author: Xiumin
 * @create: 2018/3/30
 */
@Entity
@Table(name = "kf_area", catalog = "uaa")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Area implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @Size(max = 10)
    @Column(name = "code", length = 10)
    private String code; 	// 区域编码

    @NotNull
    @Size(max = 10)
    @Column(name = "type", length = 10)
    private int type; 	// 区域类型（1：国家；2：省份、直辖市；3：地市；4：区县）

    @NotNull
    @Size(max = 20)
    @Column(name = "name", length = 20, nullable = false)
    private String name;

    @ManyToOne(fetch= FetchType.EAGER)
    @JsonBackReference
    @JoinColumn(name="parent")
    private Area parent;

    @Size(max = 100)
    @Column(name = "parent_ids", length = 100)
    private String parentIds; // 所有父级编号

    @NotNull
    @Column(name = "sort", length = 10, nullable = false)
    private Integer sort;

    @Size(max = 2)
    @Column(name = "status", length = 2, nullable = false)
    protected int status; 	// 状态标记（0：正常；1：删除）

    @OneToMany(mappedBy = "area")
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<Base> bases = new HashSet<>();

    // jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public Area code(String code) {
        this.code = code;
        return this;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Integer getType() {
        return type;
    }

    public Area type(Integer type) {
        this.type = type;
        return this;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public Area name(String name) {
        this.name = name;
        return this;
    }

    public void setName(String name) {
        this.name = name;
    }


    public Integer getSort() {
        return sort;
    }

    public Area sort(Integer sort) {
        this.sort = sort;
        return this;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public Integer getStatus() {
        return status;
    }

    public Area status(Integer status) {
        this.status = status;
        return this;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Set<Base> getBases() {
        return bases;
    }

    public Area bases(Set<Base> bases) {
        this.bases = bases;
        return this;
    }

    public Area addBase(Base base) {
        this.bases.add(base);
        base.setArea(this);
        return this;
    }

    public Area removeBase(Base base) {
        this.bases.remove(base);
        base.setArea(null);
        return this;
    }

    public void setBases(Set<Base> bases) {
        this.bases = bases;
    }
    // jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Area area = (Area) o;
        if (area.getId() == null || getId() == null) {
            return false;
        }
        return Objects.equals(getId(), area.getId());
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(getId());
    }

    @Override
    public String toString() {
        return "Area{" +
            "id=" + getId() +
            ", code='" + getCode() + "'" +
            ", type=" + getType() +
            ", name='" + getName() + "'" +
            ", parentsIds='" + parentIds + "'" +
            ", sort=" + getSort() +
            ", status=" + getStatus() +
            "}";
    }

    public Area getParent() {
        return parent;
    }

    public void setParent(Area parent) {
        this.parent = parent;
    }

    public void setParentIds(String parentIds) {
        this.parentIds = parentIds;
    }
}
